<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <!-- <el-form-item label="机构编号" prop="deptCode">
        <el-input
          v-model="queryParams.deptCode"
          placeholder="请输入机构编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="选择支行" prop="orgCode">
        <treeselect v-model="queryParams.orgCode"
          :disable-branch-nodes="true"
            search-nested
          :options="deptOptions"
          :normalizer="normalizer"
            placeholder="请选择支行"
          style="width:400px;"/>
      </el-form-item>
      <el-form-item label="录像机编号" prop="nvrId">
        <el-input
          v-model="queryParams.nvrId"
          placeholder="请输入录像机编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道号" prop="cameraChannel">
        <el-input
          v-model="queryParams.cameraChannel"
          placeholder="请输入通道号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头编号" prop="cameraId">
        <el-input
          v-model="queryParams.cameraId"
          placeholder="请输入摄像头编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头名称" prop="cameraName">
        <el-input
          v-model="queryParams.cameraName"
          placeholder="请输入摄像头名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="图片日期" prop="imageDate">
        <el-date-picker clearable
          v-model="queryParams.imageDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择图片日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['camera:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['camera:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['camera:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleImport" v-hasPermi="['camera:info:add']">导入</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['camera:info:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="支行机构" align="center" prop="deptName" />
      <el-table-column label="支行机构编码" align="center" prop="deptCode" />
      <el-table-column label="录像机编号" align="center" prop="nvrId" />
      <el-table-column label="通道号" align="center" prop="cameraChannel" />
      <el-table-column label="摄像头编号" align="center" prop="cameraId" />
      <el-table-column label="摄像头名称" align="center" prop="cameraName" />
      <!-- <el-table-column label="摄像头图片" align="center" prop="cameraImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cameraImage" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="图片日期" align="center" prop="imageDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.imageDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['camera:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['camera:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改摄像头管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="机构编号" prop="deptCode">
          <el-input v-model="form.deptCode" placeholder="请输入机构编号" />
        </el-form-item> -->
        <el-form-item label="选择支行" prop="orgCode">
          <treeselect v-model="form.orgCode"
            :disable-branch-nodes="true"
              search-nested
            :options="deptOptions"
            :normalizer="normalizer"
              placeholder="请选择支行"
            style="width:100%;"/>
        </el-form-item>
        <el-form-item label="录像机编号" prop="nvrId">
          <el-input v-model="form.nvrId" placeholder="请输入录像机编号" />
        </el-form-item>
        <el-form-item label="通道号" prop="cameraChannel">
          <el-input v-model="form.cameraChannel" placeholder="请输入通道号" />
        </el-form-item>
        <el-form-item label="摄像头编号" prop="cameraId">
          <el-input v-model="form.cameraId" placeholder="请输入摄像头编号" />
        </el-form-item>
        <el-form-item label="摄像头名称" prop="cameraName">
          <el-input v-model="form.cameraName" placeholder="请输入摄像头名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="摄像头图片" prop="cameraImage">
          <image-upload v-model="form.cameraImage"/>
        </el-form-item> -->
        <!-- <el-form-item label="图片日期" prop="imageDate">
          <el-date-picker clearable
            v-model="form.imageDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择图片日期">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="批量导入" :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="fileForm" label-width="100px">
        <el-form-item label="导入模版">
          <el-button size="small" type="primary" @click="downloadTemplate()">下载导入模版</el-button>
        </el-form-item>
        <el-form-item label="请选择文件">
          <el-upload class="upload-demo" ref="upload" accept=".xlsx, .xlsm, .xls" :auto-upload="false" action="#"
            :limit="1" :file-list="fileList" :on-change="handleChange" :on-exceed="handleExceed"
            :on-remove="handleRemove">
            <el-button size="small" type="success">选择文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleImportExcel" :disabled="uploading">确定导入</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDept } from "@/api/system/dept";
import { listInfo, getInfo, delInfo, addInfo, updateInfo, importConfig } from "@/api/iis/cameraInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Info",
  components: { Treeselect },
  dicts: ['sys_normal_disable', 'task_frequency_type', "task_scenarios_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像头管理表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deptOptions: [],
      deptList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptCode: null,
        cameraId: null,
        cameraName: null,
        cameraImage: null,
        imageDate: null,
        status: "0",
      },
      // 表单参数
      form: {
        status: "0"
      },
      // 表单校验
      rules: {
        orgCode: [
          { required: true, message: "请选择支行", trigger: "blur" }
        ],
        nvrId: [
          { required: true, message: "录像机编号不能为空", trigger: "blur" }
        ],
        cameraChannel: [
          { required: true, message: "通道号不能为空", trigger: "blur" }
        ],
        cameraId: [
          { required: true, message: "摄像头编号不能为空", trigger: "blur" }
        ],
        cameraName: [
          { required: true, message: "摄像头名称不能为空", trigger: "blur" }
        ],
        equipmentIp: [
        { required: true, validator: this.validateIP, trigger: 'blur' }
        ]
      },
      //excel批量导入功能
      dialogFormVisible: false,
      fileList: [],  //文件列表
      file: {},
      uploading: true, //默认置灰确定导入按钮
    };
  },
  created() {
    listDept().then(response => {
      this.deptList = response.data
      this.deptOptions = this.handleTree(response.data, "deptId");
    });
    this.getList();
  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 查询摄像头管理列表 */
    getList() {
      this.loading = true;
      let queryParams = Object.assign({}, this.queryParams);
      if (queryParams.orgCode){
        const dept = this.deptList.find(item => item.deptId === queryParams.orgCode)
        if (dept && dept.deptCode) {
          // return false
          queryParams.deptCode = dept.deptCode
        }
      }
      delete queryParams["orgCode"]
      listInfo(queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptCode: null,
        cameraId: null,
        cameraName: null,
        cameraImage: null,
        imageDate: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加摄像头管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        if (response.data && response.data.deptCode){
          const dept = this.deptList.find(item => item.deptCode === response.data.deptCode)
          if (dept && dept.deptId) {
            // return false
            response.data.orgCode = dept.deptId
          }
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改摄像头管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let queryParams = Object.assign({}, this.form);
          if (queryParams.orgCode){
            const dept = this.deptList.find(item => item.deptId === queryParams.orgCode)
            if (dept && dept.deptCode) {
              // return false
              queryParams.deptCode = dept.deptCode
            }else{
              this.$modal.msgError("请选择支行")
              return false
              // queryParams.deptCode = null
            }
          }
          if (queryParams.id != null) {
            updateInfo(queryParams).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(queryParams).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除摄像头管理编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('camera/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 批量导入操作 */
    handleImport() {
      this.dialogFormVisible = true;
      this.fileList = [];
      this.file = {};
      this.uploading = true; // 置灰确定导入按钮
    },
    //excel批量打入功能
    handleChange(file) {     //(file,fileList)参数
      this.uploading = false;
      this.file = file;
    },
    handleChangeBankTask(file) {     //(file,fileListBankTask)参数
      this.uploadingBankTask = false;
      this.fileBankTask = file;
    },
    //文件超出个数限制
    handleExceed() {
      this.$message.warning("仅允许上传一个文件！");
    },
    //移除文件
    handleRemove() {
      this.uploading = true;
    },
    //取消
    handleCancel() {
      this.file = {};
      this.fileList = [];
      this.dialogFormVisible = false;
      this.uploading = true;
    },
    //导入excel
    handleImportExcel() {
      if (!this.file.size) {
        this.$message.warning("请选择上传的文件");
        return;
      }
      this.uploading = true;
      let formData = new FormData();
      formData.append("file", this.file.raw);
      importConfig(formData).then(res => {
        this.$modal.msgSuccess("导入成功");
        this.uploading = false;  //调用接口
        this.dialogFormVisible = false;
        this.getList();
      });
    },
    downloadTemplate() {
      this.download('camera/info/downloadTemplate', {}, `templateCameraList.xlsx`)
    },
  }
};
</script>
